import {
    observer
} from 'mobx-react';
import {
    WeaUpload,
    WeaDialog,
    WeaSelect,
    WeaNewScroll,
    WeaMoreButton,
    WeaSearchGroup,
} from 'ecCom'
import {
    WeaSwitch
} from 'comsMobx';
import {
    Row,
    Col,
    Spin,
    Button,
} from 'antd'
import {
    IEVersion
} from '../../../../util/pure-util.js'
import {
    WeaLocaleProvider
} from 'ecCom';
import ImportResult from './ImportResult'
import ImportHistory from './ImportHistory'
const getLabel = WeaLocaleProvider.getLabel;

@observer
export default class Import extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            width: 870,
            height: 510,
            list: [],
            upStatus: '',
        }
    }

    getForm = () => {
        const {
            store
        } = this.props, {
            importDialog
        } = store, {
            condition,
            form,
        } = importDialog, {
            isFormInit
        } = form;

        let _arr = [];
        isFormInit && condition.map((c, i) => {
            let arr = [];
            if (i == 0) { //基本信息
                c.items.map((field, index) => {
                    let key = field.domkey[0];
                    let url = field.value ? field.value : '';
                    arr.push({
                        com: (
                            <Row>
                                <Col span={1} offset={6}>
                                    <div className='hrm-import-circle' style={{paddingLeft: IEVersion() == '-1' ? 6 : 7, marginTop: 15}}>
                                        <div style={{paddingTop: (IEVersion() == '11' || IEVersion() == 'edge') ? 5 : 2}}>{index + 1}</div>
                                    </div>
                                </Col>
                                <Col span={14}>
                                    <Row style={{marginTop: 15}}>
                                        <Col span={field.labelcol}>{field.label.length > 14 ? `${field.label.slice(0, 14)}...` : field.label}:</Col>
                                        <Col span={field.fieldcol}>
                                            {this.getElements(key, url, field)}
                                        </Col>
                                    </Row>
                                </Col>
                            </Row>
                        ),
                        colSpan: 1,
                    })
                })
            }
            if (i == 1) { //导入说明
                c.items.map((field, index) => {
                    let p = field.value;
                    let url = field.link ? field.link : '';
                    arr.push({
                        com: (
                            <Row>
                                <Col span={1} offset={6} style={{marginTop: 15}}>
                                    <div className='hrm-import-circle' style={{paddingLeft: IEVersion() == '-1' ? 6 : 7, paddingLeft: (index > 8) ? 3 : 6}}>
                                        <div style={{paddingTop: (IEVersion() == '11' || IEVersion() == 'edge') ? 5 : 2}}>{index + 1}</div>
                                    </div>
                                </Col>
                                <Col span={14} style={{marginTop: 15}}>
                                    {this.getImportInstructions(p, url, index)}
                                </Col>
                            </Row>
                        ),
                        colSpan: 1
                    })
                })
            }
            _arr.push(<div style={{color: '#484848'}}><WeaSearchGroup needTigger={true} title={c.title} showGroup={c.defaultshow} items={arr} col={1} /></div>)
        });
        return _arr;
    }

    getElements = (key, url, field) => {
        const {
            store
        } = this.props, {
            setExcelFileNo,
            importDialog,
            setSelectedValue
        } = store, {
            templetName,
            selectedValue,
            form,
        } = importDialog, {
            upStatus,
            list
        } = this.state;

        let dom;
        if (key == 'templet') {
            dom = this.getTempletElement(url, selectedValue, templetName);
        } else if (key == 'excelfile') {
            dom = (<div>
                        <div style={{float: 'left',marginTop: -10}}>
                            <WeaUpload
                                uploadUrl='/api/doc/upload/uploadFile'
                                category='string'
                                limitType='xls,xlsx'
                                datas={upStatus === 'uploaded'  ? list : []}
                                onChange={(id, list) => {this.setState({list:list}); setExcelFileNo(id)}}
                                onUploading={status => this.handleUploading(status)}
                            >
                                <Button>{getLabel(125333, '选择文件')}</Button>
                            </WeaUpload>
                        </div>
                        <div style={{float: 'left', paddingLeft: 10}}>
                            { list.length == 0 ? <p>{ getLabel(384040, '未选择任何文件')}</p> : list.map(file => <p>{file.filename}</p>) }
                        </div>
                    </div>)
        } else {
            dom = <WeaSwitch fieldConfig={field} form={form} formParams={form.getFormParams()} />
        }
        return dom;
    }

    getImportInstructions = (val, url, index) => {
        const {
            store
        } = this.props, {
            importDialog
        } = store, {
            templetName,
            selectedValue,
        } = importDialog;

        let p;
        if (index == 0) {
            let dom = this.getTempletElement(url, selectedValue, templetName);
            p = (<p>{ getLabel(383083, '请先下载模板')}：{dom}</p>);
        } else {
            p = (<p>{val}</p>);
        }
        return p;
    }

    getTempletElement = (url, selectedValue, templetName) => {
        let dom;
        let urlArr = url.split(';');
        if (urlArr.length > 1) {
            if (selectedValue == 'workcode') {
                dom = <a href={urlArr[0]}>{templetName}</a>;
            } else {
                dom = <a href={urlArr[1]}>{templetName}</a>;
            }
        } else {
            dom = <a href={url}>{templetName}</a>;
        }
        return dom
    }

    handleUploading = (status) => {
        this.setState({
            upStatus: status
        });

        if (status == 'uploading') {
            this.setState({
                list: []
            });
        }
    }

    getButtons = () => {
        const {
            store
        } = this.props, {
            submitExcel,
            queryRecord,
            importDialog,
        } = store, {
            upStatus
        } = this.state;

        return [
            (<Button type="primary" disabled={upStatus === 'uploaded' ? false : true} onClick={() => submitExcel()}>{ getLabel(615, '提交')}</Button>),
            (<Button type="primary" onClick={() => queryRecord()}>{getLabel(24644, '历史导入记录')}</Button>),
            (<WeaMoreButton />)
        ];
    }

    render() {
        const {
            store
        } = this.props, {
            importDialog,
            resultDialog,
            recordDialog,
            setImportDialogVisible
        } = store, {
            visible,
            title,
            loading
        } = importDialog, {
            width,
            height,
        } = this.state;

        return (
            <div>
                <WeaDialog
                    title={title}
                    icon="icon-coms-hrm"
                    iconBgcolor="#217346"
                    visible={visible}
                    closable={true}
                    onCancel={() => setImportDialogVisible(false) }
                    buttons={this.getButtons()}
                    style={{width: width, height: height}}
                    onChangeHeight={(h) => this.setState({height: h})}
                >
                    <Spin spinning={loading}>
                        <WeaNewScroll height={height}>
                            {this.getForm()}
                        </WeaNewScroll>
                    </Spin>
                </WeaDialog>
                {resultDialog.visible && <ImportResult store={store}/>}
                {recordDialog.visible && <ImportHistory store={store}/>}
            </div>
        )
    }
}